package js.web.center.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import js.web.center.core.config.JsWebConfig;
import js.web.center.script.core.JsWebScriptEngineCore;

/**
 * 静态状态获取
 * @author dowell
 *
 */
public class JsWebStaticsFileReader {
	/**
	 * 获取文件更新时间
	 * @param filePath 文件路径
	 * @return
	 */
	public static long checkTemplateTextTime(String filePath){
		File file=new File(JsWebScriptEngineCore.basePath+JsWebConfig.templatePath+filePath);
		if(file.exists()){
			return file.lastModified();
		}
		return -1;
	}
	/**
	 * 获取模板资源
	 * @param filePath 文件路径
	 * @return
	 * @throws IOException
	 */
	public static String getTemplateTextFile(String filePath) throws IOException{
		File file=new File(JsWebScriptEngineCore.basePath+JsWebConfig.templatePath+filePath);
		if(file.exists()){
			FileInputStream fileInputStream=new FileInputStream(file);
			InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream, JsWebConfig.DEFAULT_CHAR_CODE);
			BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
			String tempStr=null;
			String backStr="";
			while((tempStr=bufferedReader.readLine())!=null){
				backStr+=tempStr;
			}
			bufferedReader.close();
			inputStreamReader.close();
			return backStr;
		}else{
			return null;
		}
	}
}
